﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace NinjaToolbox.PhysicsSystem
{
    [CustomEditor(typeof(ColliderObject))]
    [CanEditMultipleObjects]
    public class ColliderObjectInspector : Editor
    {
        string[] mCachePhysicsLayerConstNames;

        PhysicsShapeContainerDrawer mPhysicsShapeContainerDrawer;
        PhysicsShapeContainerDrawer PhysicsShapeContainerDrawer
        {
            get
            {
                if (mPhysicsShapeContainerDrawer == null)
                {
                    mPhysicsShapeContainerDrawer = new PhysicsShapeContainerDrawer();
                    mPhysicsShapeContainerDrawer.Initialization((base.target as ColliderObject).PhysicsShapeContainer);
                }

                return mPhysicsShapeContainerDrawer;
            }
        }


        void OnDestroy()
        {
            if (PhysicsShapeContainerDrawer != null)
                PhysicsShapeContainerDrawer.Dispose();
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            var layerProp = serializedObject.FindProperty("mLayer");
            var layerConstInfoProp = serializedObject.FindProperty("mLayerConstInfo");
            var isDrawGizmosProp = serializedObject.FindProperty("isDrawGizmos");
            var alwaysVisibleGizmosProp = serializedObject.FindProperty("alwaysVisibleGizmos");
            var registToManagerProp = serializedObject.FindProperty("registToManager");
            var physicsShapeContainer = serializedObject.FindProperty("mPhysicsShapeContainer");

            using (var changeCheck = new EditorGUI.ChangeCheckScope())
            {
                if (layerConstInfoProp.objectReferenceValue != null)
                {
                    layerProp.intValue = PhysicsLayerDrawer.Draw("Layer Const Info", layerConstInfoProp, layerProp.intValue, ref mCachePhysicsLayerConstNames);
                }
                else
                {
                    EditorGUILayout.PropertyField(layerConstInfoProp, new GUIContent("Layer Const Info"));
                    EditorGUILayout.PropertyField(layerProp, new GUIContent("Layer"));
                }

                PhysicsShapeContainerDrawer.DrawProperty(physicsShapeContainer, targets.Length > 1);

                EditorGUILayout.PropertyField(registToManagerProp);
                EditorGUILayout.PropertyField(isDrawGizmosProp);
                EditorGUILayout.PropertyField(alwaysVisibleGizmosProp);

                if (changeCheck.changed)
                {
                    GUI.changed = true;
                    serializedObject.ApplyModifiedProperties();
                }
            }
        }
    }
}
